package com.vinyl.android.chinese.adapter;

import java.util.ArrayList;

import com.vinyl.android.chinese.R;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class DicExpandableAdapter extends BaseExpandableListAdapter {
	private ArrayList<String> groups;
	private ArrayList<ArrayList<ArrayList<String>>> children;
	private Context mContext;
	
	private SoundPool mSoundPool;
	private int mSoundBeep;
	
	

	public DicExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
		this.mContext = context;
		this.groups = groups;
		this.children = children;
		
		mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		//mSoundBeep = mSoundPool.load( context, 0, 1 );
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	public ArrayList<String> getChild(int groupPosition, int childPosition) {
		
		return children.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_child_item, null);
		}
				
		
		ArrayList<String> list = (ArrayList<String>)getChild(groupPosition, childPosition);
		String child = (String)list.get(0);
		TextView tv = (TextView)convertView.findViewById(R.id.chinese_text);
		tv.setText(child);

		child = (String)list.get(1);
		tv = (TextView)convertView.findViewById(R.id.phonetic_text);
		tv.setText(child);
		
		child = (String)list.get(2);
		tv = (TextView)convertView.findViewById(R.id.korsound_text);
		tv.setText(child);
		
		ImageButton btnPlay = (ImageButton)convertView.findViewById(R.id.btn_play);
		btnPlay.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mSoundPool.play( mSoundBeep, 1f, 1f, 0, 0, 1f );
			}
		});
		
		ImageButton btnFavorite = (ImageButton)convertView.findViewById(R.id.btn_favorite);
		btnFavorite.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				
			}
		});		
		
		
		
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String)(getGroup(groupPosition));
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_group_item, null);
		}
		
		TextView tv = (TextView)convertView.findViewById(R.id.korean_text);
		tv.setText(group);
		
		if (isExpanded) {
			tv.setTextColor(Color.WHITE);
			convertView.setBackgroundDrawable(convertView.getResources().getDrawable(R.drawable.row_on_background));
		} else {
			tv.setTextColor(Color.BLACK);
			convertView.setBackgroundDrawable(convertView.getResources().getDrawable(R.drawable.row_background));
		}
		return convertView;
	}

	public boolean hasStableIds() {
		
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return true;
	}
	
}
